package myGrid;

import java.util.Arrays;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2024/9/16-上午11:26
 * Since  :   1.0
 * Version:   1.0
 */
public class AMGraph {
    private Object[] vexs;
    private Object[][] arcs;

    private int vexNum;
    private int arcNum;
    private Boolean visited[];

    public AMGraph(Object[] vexs) {
        this.vexs = vexs;
        int length = vexs.length;
        this.vexNum = vexs.length;
        arcs = new Object[length][length];
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                arcs[i][j]=Integer.MAX_VALUE;
            }

        }
        visited=new Boolean[length];
        Arrays.fill(visited,false);

    }

    public void createUDN(Object v1,Object v2,int weight){
        int indexV1 = findIndex(v1);
        int indexV2 = findIndex(v2);
        if (indexV1==-1||indexV2==-1){
          return ;
        }
        arcs[indexV1][indexV2]=weight;
        arcs[indexV2][indexV1]=weight;
        arcNum++;

    }





    public int  findIndex(Object v){
        for (int i = 0; i < vexs.length; i++) {
            if (v==vexs[i])
                return i;
        }

        return -1;
    }

    public  void DFS(int v){
        System.out.println(vexs[v]);
        visited[v]=true;
        for (int i = 0; i <vexs.length ; i++) {
            if ((int)arcs[v][i]!=Integer.MAX_VALUE&&!visited[i]){
                DFS(i);
            }


        }


    }


    public static void main(String[] args) {
        AMGraph amGraph = new AMGraph(new Object[]{"v1", "v2", "v3","v4"});
        // 增加边
        amGraph.createUDN("v1","v2",1);
        amGraph.createUDN("v1","v3",2);
        amGraph.createUDN("v1","v4",3);
        amGraph.createUDN("v3","v4",4);
        System.out.println(Arrays.deepToString(amGraph.arcs));
        System.out.println("边的个数:" + amGraph.arcNum);
        amGraph.DFS(0);
    }

}
